package com.bw.shopping.mvvm.viewmodel;

import android.util.Log;

import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.LogUtils;
import com.bw.greendao.dao.DaoSessionUtils;
import com.bw.greendao.entity.UserDaoEntity;
import com.bw.greendao.entity.UserEntity;
import com.bw.mvvm_common.entity.BaseEntity;
import com.bw.mvvm_common.filed.UIField;
import com.bw.mvvm_common.mvvm.viewmodel.BaseViewModel;
import com.bw.mvvm_common.utils.IntentAction;
import com.bw.shopping.entity.AddAdressEntity;
import com.bw.shopping.entity.UpdateAdressEntity;
import com.bw.shopping.mvvm.model.AddAddressModel;
import com.bw.shopping.mvvm.view.AdressActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author KK
 * @description:
 * @date :2021/10/29 14:22
 */
public class AddAddressViewModel extends BaseViewModel<AddAddressModel> {

    public ObservableField<String> username = new ObservableField<>();
    public ObservableField<String> phone = new ObservableField<>();
    public ObservableField<String> adress = new ObservableField<>();
    public boolean flag;
    int id;
    @Override
    public void oncreate() {
        super.oncreate();
        EventBus.getDefault().register(this);

    }

    //保存地址
    public void saveAdressClick(){
        if(flag){
            List<UserEntity> list = DaoSessionUtils.getInstance().getSession().queryBuilder(UserEntity.class).list();
            HashMap<String, Object> map = new HashMap<>();
            map.put("shipAddress",adress.get());
            map.put("shipIsDefault",0);
            map.put("shipUserMobile",phone.get());
            map.put("shipUserName",username.get());
            map.put("userId",list.get(0).getUser_id());
            m.requestAddAddress(map)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(this);
            return;
        }else {
            List<UserEntity> list = DaoSessionUtils.getInstance().getSession().queryBuilder(UserEntity.class).list();
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("id",id);
            hashMap.put("shipAddress",adress.get());
            hashMap.put("shipIsDefault",0);
            hashMap.put("shipUserMobile",phone.get());
            hashMap.put("shipUserName",username.get());
            hashMap.put("userId",list.get(0).getUser_id());
            m.requestUpdateAdress(hashMap)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(this);
        }

    }

    public void back(){
        finishPage();
    }



    @Subscribe(sticky = true)
    public void updateAdress(IntentAction intentAction){
        if(intentAction.ation.equals("updateAdress")){
            LogUtils.e("updateAdress"+intentAction.username);
            username.set(intentAction.username);
            adress.set(intentAction.adress);
            flag = intentAction.flag;
            id = intentAction.id;
        }else if(intentAction.ation.equals("AddAdress")){
            flag = intentAction.flag;
        }
    }

    @Override
    protected void result(BaseEntity baseEntity) {
        if(baseEntity instanceof AddAdressEntity){
            showMsg("添加地址成功");
            HashMap<String, Object> map = new HashMap<>();
            map.put(UIField.PAGECLAZZ, AdressActivity.class);
            startPage(map);
            finishPage();
        }else if(baseEntity instanceof UpdateAdressEntity){
            showMsg("修改成功");
            IntentAction intentAction = new IntentAction();
            intentAction.ation = "return";
            EventBus.getDefault().postSticky(intentAction);
            HashMap<String, Object> map = new HashMap<>();
            map.put(UIField.PAGECLAZZ, AdressActivity.class);
            startPage(map);
            finishPage();
        }
    }

    @Override
    public void onDestory() {
        super.onDestory();
        EventBus.getDefault().unregister(this);
    }
}
